document.write("<script src='js/httpUtil.js'></script>");
document.write("<script src='js/common.js'></script>");
document.write("<script src='js/pagination.js'></script>");

var pageSize = 6;
var currentPage = 1;

$(document).ready(function () {
    ipWhiteList(1);
});

$("#keywordBtn").click(function () {
    ipWhiteList(1);
});


function ipWhiteList(currentPage) {
    this.currentPage = currentPage;
    var keyword = $("#keyWord").val();
    var jsonObj = {"currentPage":currentPage,"pageSize":pageSize,"keyword":keyword};
    var result=httpPost(baseUrl+"/ipWhiteList/ipWhiteList",jsonObj);
    if(result.flag==1){
        pagination(result.result.total, pageSize, currentPage, callback)

        var ipWhiteList = $('#ipWhiteList');
        ipWhiteList.empty();
        for(var i=0;i<result.result.list.length;i++){
            var list = result.result.list[i];
            var editBtn = "<button class=\"btn btn-sm btn-primary m-b-5 m-t-5\" onclick='editIPPanel(" + list.id + ")'>编辑</button>"
            var enabledBtn = editBtn+" <button class=\"btn btn-sm btn-danger m-b-5 m-t-5\"" +
                "onclick='changeStatus(" + list.id + ",0)'>禁用</button>";
            if(list.enabled!=1){
                enabledBtn = editBtn+" <button class=\"btn btn-sm btn-info m-b-5 m-t-5\"" +
                    "onclick='changeStatus(" + list.id + ",1)'>启用</button>";
            }
            enabledBtn = enabledBtn +
                " <button class=\"btn btn-sm btn-outline-danger m-b-5 m-t-5\" " +
                "onclick='deleteIPPanel(" + list.id+ "," + currentPage + ")'>删除</button>"

            ipWhiteList.append("<tr class='text-center'>" +
                "<td>"+list.ip+"</td>" +
                "<td>"+list.description+"</td>" +
                "<td>"+enabledBtn+"</td>" +
                "</tr>")
        }
    }else{
        toastr.error("系统错误",result.msg);
    }
}

function callback(targetPage,currentPage){
    if(targetPage=='prev'){
        ipWhiteList(currentPage-1);
    }else if(targetPage=='next'){
        ipWhiteList(currentPage+1);

    }
}

$("#addIPPanelBtn").click(function () {
    $("#addIPModal :input").each(function () {
        $(this).val("");
    });
    $('#addIPModal').modal('show')
})

$("#addIPBtn").click(function () {
    var ipAddress = $("#ipAddress").val();
    var description = $("#description").val();
    yorickAssert(ipAddress != "" && description != "", "请输入完整参数", "参数错误")
    var jsonObj = {
        "IP":ipAddress,
        "description":description
    }
    var result=httpPost(baseUrl+"/ipWhiteList/addIPWhiteList",jsonObj);
    if(result.flag==1) {
        toastr.success("新增IP白名单成功", "成功");
        $('#addIPModal').modal('hide');
        ipWhiteList(1);
    }else{
        toastr.error("系统错误",result.msg);
    }
})

function editIPPanel(id){
    $("#ipId").val(id)
    var ipAddress = $("#editIPAddress");
    var description = $("#editDescription");
    var jsonObj = {
        "id":id
    }
    var result = httpPost(baseUrl+"/ipWhiteList/ipWhiteListDetail",jsonObj);
    if(result.flag==1) {
        ipAddress.val(result.result.ip)
        description.val(result.result.description)

        enabledRadio("enabled", result.result.enabled)

    }else{
        toastr.error("系统错误",result.msg);
    }
    $("#editIPModal").modal('show')
}

$("#editIPBtn").click(function () {
    var id = $("#ipId").val();
    var ipAddress = $("#editIPAddress").val();
    var description = $("#editDescription").val();
    var enabled = $('[name=enabled]:checked').val();
    var jsonObj = {
        "id":id,
        "IP":ipAddress,
        "description":description,
        "enabled":enabled
    }
    var result = httpPost(baseUrl+"/ipWhiteList/editIPWhiteList",jsonObj);
    if(result.flag==1){
        toastr.success("修改IP白名单成功", "成功");
        $('#editIPModal').modal('hide');
        ipWhiteList(currentPage);
    }else{
        toastr.error("系统错误",result.msg);
    }
})


function changeStatus(id, enabled){
    var jsonObj = {
        "id":id
    }
    var result = httpPost(baseUrl+"/ipWhiteList/ipWhiteListDetail",jsonObj);
    if(result.flag==1) {
        jsonObj = {
            "id":id,
            "IP":result.result.ip,
            "description":result.result.description,
            "enabled":enabled
        }
       result = httpPost(baseUrl+"/ipWhiteList/editIPWhiteList",jsonObj);
        if(result.flag==1){
            ipWhiteList(currentPage);
        }else{
            toastr.error("系统错误",result.msg);
        }
    }else{
        toastr.error("系统错误",result.msg);
    }
}

function deleteIPPanel(id){
    $("#deleteIPId").val(id)
    $("#deleteIPModal").modal('show');
}

$("#deleteIPBtn").click(function () {
   var id = $("#deleteIPId").val()
    var jsonObj = {
        "id":id
    }
    var result = httpPost(baseUrl+"/ipWhiteList/deleteIPWhiteList",jsonObj);
    if(result.flag==1){
        toastr.success("删除IP白名单成功", "成功");
        $('#deleteIPModal').modal('hide');
        ipWhiteList(currentPage);
    }else{
        toastr.error("系统错误",result.msg);
    }
})